package com.hyt.it.ogt.kq.common.gov.utils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;

/**
 * @author 刘恒活
 * @since 2021/8/31 10:42
 */
public class ZipUtil {

    public static void zip(File fileToAdd, String zipFilePath) throws Exception {
        ZipFile zipFile = getZipFile(zipFilePath);
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        zipFile.addFile(fileToAdd, parameters);
    }

    public static void zip(ArrayList<File> fileList,String zipFilePath) throws Exception{
        ZipFile zipFile = getZipFile(zipFilePath);
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        zipFile.addFiles(fileList, parameters);
    }

    public static void zipFiles(ArrayList fileDirList, String zipFileDir) throws ZipException {
        ArrayList fileList = new ArrayList();
        for (Object fileDir : fileDirList) {
            fileList.add(new File((String) fileDir));
        }
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        ZipFile zipFile = getZipFile(zipFileDir);
        zipFile.addFiles(fileList, parameters);
    }

    private static ZipFile getZipFile(String dir) {
        ZipFile zipFile = new ZipFile(dir);
        zipFile.setCharset(Charset.forName("GBK"));
        return zipFile;
    }

    public static void zipWithPassword(ArrayList fileList, String zipFileDir, String password) throws ZipException {
        ZipFile zipFile = getZipFile(zipFileDir);
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        parameters.setEncryptFiles(true);
        //设置加密方式
        parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        zipFile.setPassword(password.toCharArray());
        zipFile.addFiles(fileList, parameters);
    }

    public static void zipFolderNotIncludeRootFolderWithPassword(String zipFileDir, File folderDir, String password)
            throws Exception {
        ZipFile zipFile = getZipFile(zipFileDir);
        //压缩方式
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        parameters.setEncryptFiles(true);
        parameters.setIncludeRootFolder(false);
        //设置加密方式
        parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        zipFile.setPassword(password.toCharArray());
        //添加指定目录
        zipFile.addFolder(folderDir, parameters);
    }


}
